import optimizeCallback from '../function/optimizeCallback'

function createPredicateIndexFinder(dir) {
  return function (array, predicate, context) {
    predicate = optimizeCallback(predicate, context, 1)
    const length = array.length

    for (let i = dir > 0 ? 0 : length - 1; i >= 0 && i < length; i += dir) {
      if (predicate(array[i])) {
        return i
      }
    }
    return -1
  }
}

export default createPredicateIndexFinder
